package com.yuyou.tas.job.service.Async;/**
 * Created by Administrator on 2018/6/14.
 */

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * Create by chengsiyuan on 2018/6/14-11:53
 **/
@EnableAsync
@Configuration
public class AsyncConfig {
    @Bean
    public AsyncTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 核心线程数
        executor.setCorePoolSize(10);
        // 最大线程数
        executor.setMaxPoolSize(20);
        // 队列容量
        executor.setQueueCapacity(3000);
        // 线程活跃时间（秒）
        executor.setKeepAliveSeconds(60);
        // 默认线程名称
        executor.setThreadNamePrefix("commJob-Thread-");
        // 拒绝策略
        //线程池和队列满之后，交由调用者线程自己执行，如果调用者线程也已经关闭，则直接抛弃改任务
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 所有任务结束后再关闭线程池
        executor.setWaitForTasksToCompleteOnShutdown(true);
        return executor;
    }

    @Bean
    public AsyncTaskExecutor feedbackTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 核心线程数
        executor.setCorePoolSize(10);
        // 最大线程数
        executor.setMaxPoolSize(20);
        // 队列容量
        executor.setQueueCapacity(3000);
        // 线程活跃时间（秒）
        executor.setKeepAliveSeconds(60);
        // 默认线程名称
        executor.setThreadNamePrefix("feedback-commJob-Thread-");
        // 拒绝策略
        //线程池和队列满之后，交由调用者线程自己执行，如果调用者线程也已经关闭，则直接抛弃改任务
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 所有任务结束后再关闭线程池
        executor.setWaitForTasksToCompleteOnShutdown(true);
        return executor;
    }
    

    
    
    
}
